
	;;
	;; Native 32 bit assembler for the swarm code.
	;; 

section .text

	;; Global stuff
global _sw_distance:function
global _sw_vec_add:function
global _sw_vec_scale:function
	
	;; Functions here

	;;
	;; _sw_distance calculates the distance between two points held
	;; by two 64 bit floating point types.
	;; C prototype:
	;;	void _sw_distance(sw_mmx_vec *, sw_mmx_vec *, double *);
	;; 
_sw_distance:
	push ebp
	mov  ebp, esp

	;; load the registers.
	push eax
	push ebx
	mov  eax, [ebp+8]
	movapd xmm0, [eax]
	mov  ebx, [ebp+12]
	movapd xmm1, [ebx]

	;; Do the calc
	subpd xmm0, xmm1
	mulpd xmm0, xmm0
	sub  esp, 16
	movupd [esp], xmm0
	addsd xmm0, [esp+8]
	sqrtsd xmm0, xmm0

	;; Write the computed value.
	mov  eax, [ebp+16]
	movsd [eax], xmm0
	
	;; Clean up and return
	add  esp, 16
	pop  ebx
	pop  eax
	pop  ebp
	ret

	;;
	;; _sw_vec_add adds two 2d mmx vectors pointed to by the args. The
	;; added value of the vectors is stored in the first vector.
	;; C prototype:
	;;	void _sw_vec_add(sw_mmx_vec *, sw_mmx_vec *);
	;; 
_sw_vec_add:	
	push ebp
	mov  ebp, esp
	push eax
	push ebx
	
	mov  eax, [ebp+8]
	movapd xmm0, [eax]
	mov  ebx, [ebp+12]
	addpd xmm0, [ebx]
	movapd [eax], xmm0
	
	pop  ebx
	pop  eax
	pop  ebp
	ret


	;;
	;; _sw_vec_scale scales a vector by the passed value.
	;; C prototype:
	;;	void _sw_vec_scale(sw_mmx_vec *, double *);
	;;
_sw_vec_scale:
	push ebp
	mov  ebp, esp
	push eax
	push ebx
		
	mov  eax, [ebp+12]
	mov  ebx, [ebp+8]

	movsd xmm1, [ebx]
	movsd xmm2, [ebx+8]

	mulsd xmm1, [eax]
	mulsd xmm2, [eax]

	movsd [ebx], xmm1
	movsd [ebx+8], xmm2

	pop  ebx
	pop  eax
	pop  ebp
	ret


